using System;

namespace GoldStone.Common.Utils {
    public static class StringUtil {
        public static readonly String[] EmptyStringArray = new string[0];

        public static string Concat(string delimiter, int startIndex, int endIndex, string[] inputs) {
            string result = "";
            for (int i = startIndex; i < endIndex; i++) {
                result += (inputs[i] + delimiter);
            }
            return result + inputs[endIndex];
        }

        public static String TrimAll(string tobefiltered, params String[] filterTable) {
            return String.Concat(tobefiltered.Split(filterTable, StringSplitOptions.RemoveEmptyEntries));
        }

        public static String TrimAll(String tobefiltered, String filterTable) {
            return TrimAll(tobefiltered, filterTable.Split(' '));
        }

        public static String DecorateUrl(String url, String prefix) {
            url = url.Replace("&amp;", "&");
            if (url.StartsWith("http://", true, null) || url.StartsWith(prefix, true, null)) {
                return url;
            }
            return new Uri(new Uri(prefix), url).AbsoluteUri;
        }

        public static String GetUrlPath(String url) {
            if (url.EndsWith("/")) {
                return url;
            }
            String path = url.ToLower().Replace("http://", "");
            int index = path.LastIndexOf('/');
            if (index < 0) {
                return url;
            }
            int rindex = path.Length - index;
            return url.Remove(url.Length - rindex + 1);
        }

        public static String Between(String content, String startFlag, String endFlag) {
            String result = content.Substring(content.IndexOf(startFlag));
            return result.Remove(result.IndexOf(endFlag));
        }

        public static String TrimScript(String html) {
            int startIndex = html.IndexOf("<script", StringComparison.OrdinalIgnoreCase);
            while (startIndex > 0) {
                try {
                    int endIndex = html.IndexOf("/script>", StringComparison.OrdinalIgnoreCase);
                    if (endIndex < 0) {
                        break;
                    }
                    html = html.Remove(startIndex, endIndex - startIndex + "/script>".Length);
                    startIndex = html.IndexOf("<script", StringComparison.OrdinalIgnoreCase);
                } catch {
                    break;
                }
            }
            return html;
        }

        public static String Diff(String baseText, String input) {
            String[] bases = baseText.Split('\n');
            String[] inputs = input.Split('\n');
            return Diff("\n", bases, inputs);
        }

        public static string Diff(String delimiter, string[] bases, string[] inputs) {
            int startIndex, endIndex;
            Diff(bases, inputs, out startIndex, out endIndex);
            return Concat(delimiter, startIndex, endIndex, inputs);
        }

        public static void Diff(string[] bases, string[] inputs, out int startIndex, out int endIndex) {
            for (startIndex = 0; startIndex < Math.Min(bases.Length, inputs.Length); startIndex++) {
                if (bases[startIndex] != inputs[startIndex]) {
                    break;
                }
            }
            for (endIndex = 0; endIndex < Math.Min(bases.Length, inputs.Length); endIndex++) {
                if (bases[bases.Length - endIndex - 1] != inputs[inputs.Length - endIndex - 1]) {
                    break;
                }
            }
            endIndex = inputs.Length - endIndex - 1;
        }

        public static string GetHtmlBody(string htmlText) {
            try {
                return Between(htmlText, "<body", "/body>");
            } catch {
                try {
                    return Between(htmlText, "<BODY", "/BODY>");
                } catch {
                    return Between(htmlText, "<Body", "/Body>");
                }
            }
        }
    }
}